package edu.whu.powersoap;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

import java.io.File;

public class PoiProvider extends ContentProvider {

	private static final String TAG = "PoiProvider";

	public static final String AUTHORITY = "powersoap.poi";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/");

	public static final String VOLUMN_SHEET_NAME = "sheet"; // 表单名
	public static final String VOLUMN_GEO_ID = "geo_id"; // 空间对象ID
	public static final String VOLUMN_DEVICE_ID = "dev_id"; // 设备编号
	public static final String VOLUMN_SYSTEM_ID = "system_id"; // 业务系统ID
	public static final String VOLUMN_NAME = "name"; // 名称
	public static final String VOLUMN_FEEDER_LINE = "feeder_line"; // 所属馈线
	public static final String VOLUMN_TOWER_MATERIAL = "tower_material"; // 杆塔材质
	public static final String VOLUMN_TOWER_TYPE = "tower_type"; // 杆塔性质
	public static final String VOLUMN_HIGH_LOW_VOLTAGE = "high_low_voltage"; // 高低压同杆
	public static final String VOLUMN_CIRCUIT_COUNT = "circuit_count"; // 同杆架设回路数
	public static final String VOLUMN_OPERATING_COMPANY = "operating_company"; // 运行单位
	public static final String VOLUMN_MAINTAIN_GROUP = "maintain_group"; // 维护班组
	public static final String VOLUMN_OPERATING_STATUS = "operating_status"; // 运行状态
	public static final String VOLUMN_ASSET_COMPANY = "asset_company"; // 资产单位
	public static final String VOLUMN_ASSET_PROPERTY = "asset_property"; // 资产性质
	public static final String VOLUMN_URBAN_RURAL = "urban_rural"; // 城农网
	public static final String VOLUMN_GEOOID = "geooid"; // GEOOID
	public static final String VOLUMN_X = "x"; // X
	public static final String VOLUMN_Y = "y"; // Y

	private static final String DATABASE_NAME = "poi.db";
	private static final int DATABASE_VERSION = 2;

	private static final String TABLE_NAME = "poi";

	private DatabaseHelper mOpenHelper;

	@Override
	public boolean onCreate() {
		mOpenHelper = new DatabaseHelper(getContext(), DATABASE_NAME, null,
				DATABASE_VERSION);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();

		Cursor cursor = db.query(TABLE_NAME, projection, selection,
				selectionArgs, null, null, sortOrder);

		return cursor;
	}

	@Override
	public String getType(Uri uri) {

		Log.i(TAG, "getType---uri=" + uri);
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		SQLiteDatabase db = mOpenHelper.getWritableDatabase();

		/* now insert into database */
		long rowId = db.insert(TABLE_NAME, null, values);
		if (rowId > 0) {
			uri = ContentUris.withAppendedId(uri, rowId);
			sendNotify(uri);
			return uri;
		}
		throw new SQLException("Failed to insert row into: " + uri);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count = db.delete(TABLE_NAME, selection, selectionArgs);
		if (count > 0)
			sendNotify(uri);

		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count = db.update(TABLE_NAME, values, selection, selectionArgs);

		sendNotify(uri);

		return count;
	}

	private void sendNotify(Uri uri) {
		getContext().getContentResolver().notifyChange(uri, null);

	}

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			StringBuilder sql = new StringBuilder();
			sql.append("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (");
			sql.append(VOLUMN_SHEET_NAME + " TEXT, ");
			sql.append(VOLUMN_GEO_ID + " TEXT, ");
			sql.append(VOLUMN_DEVICE_ID + " TEXT, ");
			sql.append(VOLUMN_SYSTEM_ID + " TEXT, ");
			sql.append(VOLUMN_NAME + " TEXT, ");
			sql.append(VOLUMN_FEEDER_LINE + " TEXT, ");
			sql.append(VOLUMN_TOWER_MATERIAL + " TEXT, ");
			sql.append(VOLUMN_TOWER_TYPE + " TEXT, ");
			sql.append(VOLUMN_HIGH_LOW_VOLTAGE + " TEXT, ");
			sql.append(VOLUMN_CIRCUIT_COUNT + " TEXT, ");
			sql.append(VOLUMN_OPERATING_COMPANY + " TEXT, ");
			sql.append(VOLUMN_MAINTAIN_GROUP + " TEXT, ");
			sql.append(VOLUMN_OPERATING_STATUS + " TEXT, ");
			sql.append(VOLUMN_ASSET_COMPANY + " TEXT, ");
			sql.append(VOLUMN_ASSET_PROPERTY + " TEXT, ");
			sql.append(VOLUMN_URBAN_RURAL + " TEXT, ");
			sql.append(VOLUMN_GEOOID + " TEXT, ");
			sql.append(VOLUMN_X + " DOUBLE, ");
			sql.append(VOLUMN_Y + " DOUBLE)");

			db.execSQL(sql.toString());
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrade database from " + oldVersion + " to "
					+ newVersion);

			Log.w(TAG, "drop all data!");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);

		}

	}

	public static void addPoi(Context context, PoiBean poi) {

		ContentResolver cr = context.getContentResolver();

		/* insert new */
		ContentValues values = new ContentValues();
		values.put(VOLUMN_SHEET_NAME, poi.sheet);
		values.put(VOLUMN_GEO_ID, poi.geo_id);
		values.put(VOLUMN_DEVICE_ID, poi.dev_id);
		values.put(VOLUMN_SYSTEM_ID, poi.system_id);
		values.put(VOLUMN_NAME, poi.name);
		values.put(VOLUMN_FEEDER_LINE, poi.feeder_line);
		values.put(VOLUMN_TOWER_MATERIAL, poi.tower_material);
		values.put(VOLUMN_TOWER_TYPE, poi.tower_type);
		values.put(VOLUMN_HIGH_LOW_VOLTAGE, poi.high_low_voltage);
		values.put(VOLUMN_CIRCUIT_COUNT, poi.circuit_count);
		values.put(VOLUMN_OPERATING_COMPANY, poi.operating_company);
		values.put(VOLUMN_MAINTAIN_GROUP, poi.maintain_group);
		values.put(VOLUMN_OPERATING_STATUS, poi.operating_status);
		values.put(VOLUMN_ASSET_COMPANY, poi.asset_company);
		values.put(VOLUMN_ASSET_PROPERTY, poi.asset_property);
		values.put(VOLUMN_URBAN_RURAL, poi.urban_rural);
		values.put(VOLUMN_GEOOID, poi.geooid);
		values.put(VOLUMN_X, poi.x);
		values.put(VOLUMN_Y, poi.y);

		cr.insert(CONTENT_URI, values);

		return;
	}
	


}
